package com.tian.arch.function.http

import com.tian.arch.util.GsonUtils
import java.io.Serializable

open class Resp<T>(
    var responseTime: String,
    var code: String,
    var msg: String?,
    var data: T?,
    var timestamp: String?,
    var path: String?,
    var status: Int?,
    var error: String?,
    var message: String?,
    var requestId: String?,
) : Serializable {
    fun isSuccessful() = code == "200"
}

fun <T : Resp<*>> T.check(): T {
    return if (isSuccessful()) {
        this
    } else {
        if (this.status != null) {
            throw FormatException(GsonUtils.toJson(this))
        }
        throw ApiException(code, msg)
    }
}
